using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

// [System.Serializable, VolumeComponentMenu("CG/VoxelClouds")]
public class HoudiniCloudsVolume : VolumeComponent, IPostProcessComponent
{
    [Space(10)] [Header("调试模式")] [Tooltip("光遇的数据采集模式")]
    public BoolParameter skyMode = new BoolParameter(false);

    [Tooltip("Debug模式")] public ClampedIntParameter debugMode = new ClampedIntParameter(6, 0, 6);
    [Tooltip("是否输出迭代次数")] public BoolParameter debugIteration = new BoolParameter(false);

    [Space(10)] [Header("Pass降采样")] public ClampedIntParameter passOneDownSample = new ClampedIntParameter(4, 1, 16);
    public ClampedIntParameter passTwoDownSample = new ClampedIntParameter(2, 1, 16);
    public ClampedIntParameter passThreeDownSample = new ClampedIntParameter(1, 1, 16);
    public ClampedIntParameter passDepthDownSample = new ClampedIntParameter(4, 1, 16);

    [Space(10)] [Header("光线步进")] [Tooltip("废弃的精度采样方式")]
    public ClampedFloatParameter accuracy = new ClampedFloatParameter(0.1f, 0.0f, 1.0f);

    [Space(10)] [Header("贴图")] public Texture3DParameter index = new Texture3DParameter(null);
    public Texture3DParameter data = new Texture3DParameter(null);
    public Texture3DParameter noise = new Texture3DParameter(null);

    [Space(10)] [Header("距离场")] [Tooltip("体积乘数")]
    public IntParameter highResScale = new IntParameter(16);

    [Tooltip("CloudScale")] public FloatParameter cloudScale = new FloatParameter(100f);
    [Tooltip("x: 近裁剪平面 y:远裁剪平面")] public Vector2Parameter clipPlane = new Vector2Parameter(new Vector2(20.0f, 7.0f));

    [Space(10)] [Header("云的运动")] public FloatParameter sinSpeed = new FloatParameter(2.0f);
    public FloatParameter sinStrength = new FloatParameter(2.0f);
    public FloatParameter noiseSpeed = new FloatParameter(2.0f);
    public FloatParameter noiseStrength = new FloatParameter(2.0f);
    public Vector3Parameter fbm = new Vector3Parameter(new Vector4(0.25f, 0.5f, 1.0f));
    public FloatParameter fbmFreq = new FloatParameter(2.0f);

    [Space(10)] [Header("云的光照")] [Tooltip("xyz,w, 只与颜色相关")]
    public Vector4Parameter colorAdjust = new Vector4Parameter(new Vector4(0.1f, 0.1f, 0.1f, 0.4f));

    [Tooltip("xyz: 天空颜色强度")] public ClampedFloatParameter skyColorStrength = new ClampedFloatParameter(1.0f, 0.0f, 5.0f);
    [Tooltip("xyz: 天空颜色值")] public ColorParameter skyColor = new ColorParameter(Color.blue);
    [Tooltip("xyz: 天空颜色强度")] public ClampedFloatParameter cloudColorStrength = new ClampedFloatParameter(1.0f, 0.0f, 5.0f);
    [Tooltip("xyz: 云的颜色值")] public ColorParameter cloudColor = new ColorParameter(Color.grey);
    [Tooltip("阴影强度")] public ClampedFloatParameter shadowStrength = new ClampedFloatParameter(2.0f, -5.0f, 5.0f);

    [Space(10)] [Header("云的散射")] public ClampedFloatParameter forwardScattering = new ClampedFloatParameter(0.8f, 0f, 1f);
    public ClampedFloatParameter backScattering = new ClampedFloatParameter(0.2f, 0f, 1f);
    public ClampedFloatParameter baseBrightness = new ClampedFloatParameter(0.2f, 0f, 1f);
    public ClampedFloatParameter phaseFactor = new ClampedFloatParameter(0.3f, 0f, 1f);

    // [Tooltip("xyz: 色彩调整")] public Vector3Parameter emissionColor = new Vector3Parameter(Vector3.zero);

    // [Header("阴影")] public RenderTextureParameter shadowRt = new RenderTextureParameter(null);
    // public FloatParameter lookupOffset = new FloatParameter(10);
    // public FloatParameter far = new FloatParameter(200f);
    // public FloatParameter size = new FloatParameter(10f);
    // public IntParameter textureSize = new IntParameter(2560);
    // public FloatParameter bias = new FloatParameter(0.003f);
    // public FloatParameter strength = new FloatParameter(0.5f);


    public bool IsActive()
    {
        // Volume 相关判空在此，避免组件为激活但被渲染
        if (index.value == null || !index.overrideState)
        {
            // Debug.Log("HoudiniCloudsVolume没有指定Index贴图");
            return false;
        }

        if (data.value == null || !data.overrideState)
        {
            // Debug.Log("HoudiniCloudsVolume没有指定Data贴图");
            return false;
        }

        return true;
    }

    public bool IsTileCompatible() => false;

    public void OnValidate()
    {
        //数值校验，强制开启
        shadowStrength.overrideState = active;
        skyMode.overrideState = active;
        debugMode.overrideState = active;
        debugIteration.overrideState = active;
        passOneDownSample.overrideState = active;
        passTwoDownSample.overrideState = active;
        passThreeDownSample.overrideState = active;
        passDepthDownSample.overrideState = active;
        accuracy.overrideState = active;
        index.overrideState = active;
        data.overrideState = active;
        noise.overrideState = active;
        highResScale.overrideState = active;
        cloudScale.overrideState = active;
        clipPlane.overrideState = active;
        sinSpeed.overrideState = active;
        sinStrength.overrideState = active;
        noiseSpeed.overrideState = active;
        noiseStrength.overrideState = active;
        fbm.overrideState = active;
        fbmFreq.overrideState = active;
        colorAdjust.overrideState = active;
        skyColorStrength.overrideState = active;
        skyColor.overrideState = active;
        cloudColorStrength.overrideState = active;
        cloudColor.overrideState = active;
        shadowStrength.overrideState = active;
        forwardScattering.overrideState = active;
        backScattering.overrideState = active;
        baseBrightness.overrideState = active;
        phaseFactor.overrideState = active;
    }
}